﻿using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.EditorExt;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.GlobeCore;
using ESRI.ArcGIS.NetworkAnalysis;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;

namespace DIWAGIS.ApplicationLayer.EditorTools
{
    /// <summary>
    /// Command that works in ArcMap/Map/PageLayout
    /// </summary>
    [Guid("ef76ebe8-edfb-4826-a3ca-bc98858b8226")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("DIWAGIS.ApplicationLayer.EditorTools.cmdEditTool")]
    public sealed class cmdEditTool: BaseCommand
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Register(regKey);
            ControlsCommands.Register(regKey);
        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Unregister(regKey);
            ControlsCommands.Unregister(regKey);
        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper = null;
        private IMapControl4 m_mapcontrol;
        private IEngineEditor m_editor;
        private IWorkspaceEdit m_workspaceEdit;
        public IWorkspaceEdit workspaceEdit
        {
            get { return m_workspaceEdit; }
            set { m_workspaceEdit = value; }
        }

        public cmdEditTool(IEngineEditor editor)
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = "Chọn đối tượng"; //localizable text
            base.m_caption = "Chọn đối tượng";  //localizable text 
            base.m_message = "Chọn đối tượng";  //localizable text
            base.m_toolTip = "Chọn đối tượng";  //localizable text
            base.m_name = "Chọn đối tượng";   //unique id, non-localizable (e.g. "MyCategory_MyCommand")

            m_editor = editor;
            try
            {
                //
                // TODO: change bitmap name if necessary
                //
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overridden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            try
            {
                m_hookHelper = new HookHelperClass();
                m_hookHelper.Hook = hook;
                if (m_hookHelper.ActiveView == null)
                    m_hookHelper = null;
            }
            catch
            {
                m_hookHelper = null;
            }

            if (m_hookHelper == null)
                base.m_enabled = false;
            else
                base.m_enabled = true;

            // TODO:  Add other initialization code
            if (m_hookHelper.Hook is IMapControl4)
            {
                m_mapcontrol = (IMapControl4)m_hookHelper.Hook;
            }
            else if (m_hookHelper.Hook is IToolbarControl2)
            {
                IToolbarControl2 _toolBar;
                _toolBar = (IToolbarControl2)m_hookHelper.Hook;
                m_mapcontrol = (IMapControl4)_toolBar.Buddy;
            }
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add cmdEditTool.OnClick implementation
            try
            {
                //if (m_workspaceEdit != null)
                //{
                //    if (m_workspaceEdit.IsBeingEdited() == true)
                //    {
                //        ICommand m_EditTool = new ControlsEditingEditToolClass();
                //        m_EditTool.OnCreate(m_mapcontrol);
                //        m_mapcontrol.CurrentTool = m_EditTool as ITool;
                //    }
                //}
                //else
                //{
                if (m_editor.EditState == esriEngineEditState.esriEngineStateEditing)
                {
                    ICommand m_EditTool = new ControlsEditingEditToolClass();
                    m_EditTool.OnCreate(m_mapcontrol);
                    m_mapcontrol.CurrentTool = m_EditTool as ITool;
                }
                //}
            }
            catch (Exception ex)
            {
                Class.clsStaticFunctions.ThongBaoLoi(ex);
            }
        }

        #endregion

        public override bool Enabled
        {
            get
            {
                if (m_workspaceEdit != null)
                {
                    if (m_workspaceEdit.IsBeingEdited() == false)
                        return false;
                }
                else
                {
                    //check whether Editing 
                    if (m_editor.EditState == esriEngineEditState.esriEngineStateNotEditing)
                    {
                        return false;
                    }
                }
                //conditions have been met so enable the tool
                return true;
            }
        }
    }
}
